package com.algorithm.onehundred2onehundredfifty;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author tuzhijin
 * @version Id: TwoSum_167.java, v 0.1 2021/6/6 上午8:40 tuzhijin Exp $$
 */
public class TwoSum_167 {

    public static void main(String[] args) {
        int[] numbers = {2,7,11,15};
        int target = 9;
        System.out.println(Arrays.toString(twoSum(numbers, target)));
    }

    public static int[] twoSum(int[] numbers, int target) {
        if (numbers.length==0 || numbers.length==1){
            return new int[]{-1};}
        Map<Integer, Integer> param = new HashMap<>();
        for (int i = 0; i < numbers.length; i++) {
            int temp = target - numbers[i];
            // 是否包含差值
            if (param.containsKey(temp)){
                Integer result = param.get(temp);
                return new int[]{result,i+1};
            }else {
                // 存的是当前值和位置
                param.put(numbers[i], i+1);
            }
        }
        return new int[]{-1};
    }

}
